// pages/mygroup/mygroup.js
var app= getApp();//引用app.js
var commonURL=app.data.baseURL;
var page=1;
Page({
  data:{
    "navTab":["组团中","组团成功","组团失败"],
    "currentNavtab":"0",
    "groupNow":[
      // {
      //   gid:"",
      //   tuan_id:"",
      //   time1:"2017-01-18",
      //   time2:"15:36:22",
      //   orderNum:"333333",
      //   name:"202",
      //   people:"2000人",
      //   price:"0.01",
      //   unit:"斤"
      // }
    ],
    "groupSucc":[],
    "groupErr":[]
  },
  onLoad:function(options){

  },
  switchTab: function(e){
    var that=this;
    page=1;
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    var item=e.currentTarget.dataset.idx;
    var status="";
    switch (item){
      case 0:
        status="3";break;
      case 1:
        status="2";break;
      case 2:
        status="1";break;       
    }
    wx.request({
      url: commonURL+'op=group_list', 
      data: {
        page:page,
        pagesize:"10",
        openid:app.data.openid,
        status:status
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        var data=res.data.list;
        if(!data){
          return false;
        }
        wx.showToast({
          title: '团数据加载中...',
          icon: 'loading',
          duration: 700
        })
        var arr=[];
        if(data.length>0){
          for(var key in data){
            var obj={};
            obj.id=data[key].gid;
            obj.img=data[key].share_image;
            obj.starttime=data[key].starttime;
            obj.tuan_id=data[key].groupnumber;
            obj.sumPrice=data[key].gprice;
            obj.name=data[key].gname;
            obj.num=data[key].groupnum;
            obj.unit=data[key].unit;
            arr.push(obj);
          }
          if(item=="0"){
            that.setData({
              groupNow:arr
            })
          }else if(item=="1"){
            that.setData({
              groupSucc:arr
            })
          }else if(item=="2"){
            that.setData({
              groupErr:arr
            })
          }
        }
      }
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    var that=this;
    wx.request({
      url: commonURL+'op=group_list', 
      data: {
        openid:app.data.openid,
        status:"3"
      },
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        var data=res.data.list;
        var arr=[];
        wx.showToast({
          title: '团数据加载中...',
          icon: 'loading',
          duration: 700
        })
        if(data!=null){
          for(var key in data){
            var obj={};
            obj.id=data[key].gid;
            obj.img = data[key].share_image;
            obj.starttime=data[key].starttime;
            obj.tuan_id=data[key].groupnumber;
            obj.sumPrice=data[key].gprice;
            obj.name=data[key].gname;
            obj.num=data[key].groupnum;
            obj.unit=data[key].unit;
            arr.push(obj);
          }
        }
        that.setData({
          "groupNow":arr
        });
      }
    })
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  gotoDetail:function(e){
    //跳转团详情并传参
    wx.navigateTo({
      url:"../mygroup_detail/mygroup_detail?tuan_id="+e.currentTarget.dataset.tuanid+"&goodsid="+e.currentTarget.dataset.goodsid
    })
  },
  // 上拉加载回调接口
  onReachBottom: function () {
      var that=this;
      var status="";
    switch (that.data.currentNavtab){
      case "0":
        status="3";break;
      case "1":
        status="2";break;
      case "2":
        status="1";break;       
    }
      page++;
      wx.request({
        url: commonURL+'op=group_list', 
        data: {
          page:page,
          pagesize:"10",
          openid:app.data.openid,
          status:status
        },
        header: {
            'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res.data)
          var data=res.data;
          if(data.length<1||!data.length){
            wx.showToast({
              title: '全部团数据加载完成',
              icon: 'success',
              duration: 1000
            })
            return false;
          }
          wx.showToast({
            title: '团数据加载中...',
            icon: 'loading',
            duration: 700
          })
          var arr=[];
          if(data.length>1){
            for(var key in data){
              var obj={};
              obj.id=data[key].id;
              obj.tuan_id=data[key].tuan_id;
              obj.orderNum=data[key].orderno;
              obj.zhuangtai=that.zhuangtai(data[key].status);
              obj.freight=data[key].freight;
              obj.sumPrice=data[key].pay_price;
              obj.name=data[key].goodsname;
              obj.num=data[key].gnum;
              obj.price=data[key].goodsprice;
              obj.lists_guige=data[key].optionname;
              arr.push(obj);
            }
          }
      // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
      // 上拉时需把total在原来的基础上加上count，代表从count条后的数据开始请求。
          if(status=="3"){
            that.setData({
              groupNow:that.data.groupNow.concat(arr)
            })
          }else if(status=="2"){
            that.setData({
              groupSucc:that.data.groupSucc.concat(arr)
            })
          }else if(status=="1"){
            that.setData({
              groupErr:that.data.groupErr.concat(arr)
            })
          }
        }
      })
  },
})